package com.smart.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smart.constants.SmartConstants;
import com.smart.util.JSApiTicketManager;
import com.smart.util.JSSDKSignUtil;

@Controller
public class JSSDKController {
	@RequestMapping(value="/views/wechat/get_js_args", method = RequestMethod.POST)
    public void getJSArgs(HttpServletRequest req,HttpServletResponse resp)
    {
		System.out.println("getJSArgs method start..");
        String url=req.getParameter("url");
		//		String webroot=req.getContextPath();
        url="http://"+SmartConstants.Domain_Name+url;
        System.out.println("uri:"+url);
		String jsapi_ticket=JSApiTicketManager.getJSApiTicket().getTicket();
         Map<String,String> map=JSSDKSignUtil.sign(jsapi_ticket, url);
         JSONObject jsonObject=new JSONObject();
         String nonceStr=map.get("nonceStr");
         String signature=map.get("signature");
         String timestamp=map.get("timestamp");
         jsonObject.put("signature", signature);
         jsonObject.put("nonceStr", nonceStr);
         jsonObject.put("timestamp", timestamp);
         jsonObject.put("appId",SmartConstants.APPID);
         System.out.println(jsonObject);
         PrintWriter out=null;
         try {
			out=resp.getWriter();
			out.print(jsonObject);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
         finally
         {
        	 if(out!=null) out.close();
         }
         System.out.println("getJSArgs method finish..");
    }
}
